/*
 * @Author: myw
 * @Email: 516378746@qq.com
 * @Date: 2020-02-24 08:48:29
 * @Descripttion: 
 */
import { Component, OnInit } from '@angular/core';

import { SideMenuService } from "./side-menu.service"
import { Location } from '@angular/common';
import { Router, ActivationEnd } from '@angular/router';

@Component({
    selector: 'app-side-menu',
    templateUrl: './side-menu.component.html',
    styleUrls: ['./side-menu.component.scss']
})
export class SideMenuComponent implements OnInit {

    menuList = []
    activeRoute : string = "";

    constructor(
        private sideMenuService: SideMenuService,
        private location: Location,
        private router : Router
    ) { }

    ngOnInit() {
        this.router.events.subscribe(event=>{
            if( event instanceof ActivationEnd ){
                let _config = event.snapshot.routeConfig;
                this.activeRoute = this.router.url;
                /* if( _config.path !== '' && _config.data.isLastLevel ){
                    console.log( event.snapshot.routeConfig);
                    this.editDefaultMenu();
                }  */
                this.editDefaultMenu();
            }
        })
        this.getList()
    }

    getList() {
        this.sideMenuService.apiGetList().subscribe(res => {
            this.menuList = res;
            this.editDefaultMenu();
        })
    }
    editDefaultMenu() {
        let _url = this.location.path();
        let _arrUrl = _url.split("/");
        _arrUrl.shift();
        _arrUrl.pop();
        _(this.menuList);
        function _(data) {
            data.forEach(item => {
                if (_arrUrl.indexOf(item.path) > -1) {
                    item.open = true;
                }else{
                    item.open = false;
                }
                if (item.children && item.children.length > 0) {
                    _(item.children)
                }
            });
            return data
        }
    }
    submenuChange(routerInfo) {
        _(this.menuList);
        function _(data) {
            data.forEach(item => {
                if( item.level == routerInfo.level ){
                    item.open = true;
                }
                if (item.path != routerInfo.path && item.level == routerInfo.level) {
                    item.open = false;
                } else {
                    if (item.children && item.children.length > 0) {
                        _(item.children)
                    }
                }
            })
        }
    }
}
